"""
元组的使用
"""

# 定义元组字面量 (element, element, ..., element)
('Blue', 'Yui', 1, 'Java', 'Python', True)

# 定义元组变量 tuple = (element, element, ..., element)
tuple_variable_1 = ('Blue', 'Yui', 'Java', 'Python', 'Oracle')

# 定义空元组
tuple_variable_2 = ()
tuple_variable_3 = tuple()

print(f'tuple_1 的类型是: {type(tuple_variable_1)} 内容是: {tuple_variable_1}', )
print(f'tuple_2 的类型是: {type(tuple_variable_2)} 内容是: {tuple_variable_2}', )
print(f'tuple_3 的类型是: {type(tuple_variable_3)} 内容是: {tuple_variable_3}', )

# 定义单个元素
tuple_variable_4 = ('Hello',)
print(f'tuple_4 的类型是: {type(tuple_variable_4)} 内容是: {tuple_variable_4}', )

# 定义嵌套的元组
tuple_variable_5 = ((1, 2, 3), (4, 5, 6))
print(f'tuple_5 的类型是: {type(tuple_variable_5)} 内容是: {tuple_variable_5}', )
print()

# 通过下标索引取出内容元素
number = tuple_variable_5[1][1]
print(f'从嵌套元组中取出的元素为: {number}')
print()

# 元组的相关操作
# index(element) 方法, 查找某个数据,如果数据存在返回对应的下标, 否则程序报错
tuple_variable_6 = ('Blue', 'Yui', 'Java', 'Python', 'Oracle')
index = tuple_variable_6.index('Java')
print(f'在元组 tuple_variable_6 中查找 Java 的下标是: {index}')
print()

# count(element) 方法, 统计某个数据在当前元组中出现的次数
tuple_variable_7 = ('Blue', 'Yui', 'Java', 'Python', 'Yui', 'Oracle',)
count = tuple_variable_7.count('Yui')
print(f'在元组 tuple_variable_7 中 Yui 元素出现次数为: {count}')
print()

# len(tuple) 方法, 统计元组内的元素个数
tuple_variable_8 = ('Blue', 'Yui', 'Java', 'Python', 'Oracle')
length = len(tuple_variable_8)
print(f'tuple_variable_8 元组中的元素有 {length} 个')
print()

# 无法修改 tuple 容器里面的元素
tuple_variable_10 = ('Blue', 'Yui', 'Java', 'Python', 'Oracle')
# TypeError: 'tuple' object does not support item assignment
# tuple_variable_10[1] = 'Yue'（非法操作）

# 遍历元组
# 用 while 遍历元组
tuple_variable_9 = ('Blue', 'Yui', 'Java', 'Python', 'Oracle')
index = 0
while index < len(tuple_variable_9):
    print(tuple_variable_9[index], end=' ')
    index += 1
print()
print()

# 用 for 遍历元组
for element in tuple_variable_9:
    print(element, end=' ')
print()
